package com.bigluo.common.exceptionhandler;

import com.bigluo.api.BaseResponse;
import com.bigluo.common.exception.ValidateException;
import com.bigluo.util.CustomApplicationContextUtil;
import com.bigluo.util.CustomHttpMessageConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by luojun on 2017/9/1.
 * 通用异常捕获类
 * 对于spring mvc接口所有抛出的异常都统一在此类中捕获
 */
public class CommonExceptionHandler implements HandlerExceptionResolver{
    @Autowired
    private ServletContext servletContext;
    private Logger logger = LoggerFactory.getLogger(CommonExceptionHandler.class);
    BaseResponse baseRsp = new BaseResponse();
    public ModelAndView resolveException(HttpServletRequest req, HttpServletResponse rsp, Object obj, Exception ex) {
//        WebApplicationContext tx = WebApplicationContextUtils.getWebApplicationContext(req.getSession().getServletContext());
//        UserService se = tx.getBean(UserService.class);
        //ContextLoader.getCurrentWebApplicationContext().getServletContext()
//        CustomHttpMessageConverter customHttpMessageConverter22 = CustomApplicationContextUtil.getBean(CustomHttpMessageConverter.class);
        //获取spring mvc容器中的bean
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext, "org.springframework.web.servlet.FrameworkServlet.CONTEXT.springmvc");
        CustomHttpMessageConverter customHttpMessageConverter = webApplicationContext.getBean(CustomHttpMessageConverter.class);
        try{
            if(ex!=null) {
                if(ex instanceof ValidateException) {
                    ValidateException subEx = (ValidateException)ex;
                    baseRsp.setRspCode(subEx.getCode());
                    baseRsp.setRspMsg(subEx.getMsg());
                    logger.error("validate异常",subEx);
                }else{
                    baseRsp.setRspCode("T9999");
                    baseRsp.setRspMsg("系统异常");
                    logger.error("系统异常",ex);
                }
            }else{
                logger.info("^_^大赞一个程序中没有出现异常^_^");
            }
            customHttpMessageConverter.write(baseRsp, MediaType.ALL,new ServletServerHttpResponse(rsp));
        }catch(Exception e) {
            CustomHttpMessageConverter bean = CustomApplicationContextUtil.getBean(CustomHttpMessageConverter.class);
            logger.error("sring mvc异常统一捕获失败",e);
        }
        return new ModelAndView();
    }
}
